<template>
  <div>
    <!--条件筛选-->
    <TopFilter ref="topFilter" :list="topFilterList" :cols="3" labelWidth="110" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :isSelectColumn="true"
      :onRowSelectChange="selectChange"
      :onColumnsChange="columns => this.columns = columns"
    >
      <template slot="controls">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="detailHandle">新增</el-button>
        <el-button size="small" type="primary" @click="updateEnable" :loading="saveLoading" :disabled="isOpen">启用</el-button>
        <el-button size="small" type="primary" @click="updateStop" :loading="saveLoading" :disabled='isOpen'>禁用</el-button>
      </template>
    </FilterTable>

    <BaseDialog
      :visible.sync="issearch"
      :title="'经销商查询'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <ownerSeacher  @close="dealercloseHandler" />
    </BaseDialog>

    <!-- 编辑窗口 -->
    <BaseDialog
      :visible.sync="visibleDrawer"
      :title="title"
      width="30%"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <update-storehouse v-model="initData" :type="handleType" :rowData="formData" @close="infoClose" />
    </BaseDialog>
  </div>
</template>

<script>
  import { dictionary } from '@/utils/dictMixin';
  import {getStorehouseDealer,piliangqiyong,piliangtingyong} from '@/api/partsmanage/partWarehouseManage/storehouseDealer/index';
  import ownerSeacher from '@/pages/partsmanage/claim/approve/ownerSeacher';
  import {getAllUser} from '@/api/customerRepair/activityVisit/resultEntry/index';
  import updateStorehouse from './updateStorehouse';
  import { getCurrentUser } from '@/api/cms-api/origin/construct/dealer';
  export default {
    name: "storehouseDealer",
    mixins: [dictionary],
    components: {ownerSeacher,updateStorehouse},
    data() {
      return{
        topFilterList: this.createTopFilterList(),
        columns: this.createTableColumns(),
        fetchApi: getStorehouseDealer,
        fetchParams: {xhrAbort: true},
        visibleDrawer: false,
        saveLoading: false,
        isOpen: true,
        issearch: false,
        handleType: '',
        formData: {},
        title: '',
        initData: {},
        rowSelectData: []
      };
    },
    async created() {
      this.getDictData();
      this.columns.find(x => x.dataIndex === 'bjpjsx').hidden = true;
      this.columns.find(x => x.dataIndex === 'bjpjsx').dictItems = this.createDictList('5000');
      // 获取当前登录人信息
      await getCurrentUser({}).then(response => {
        console.info(response);
        if (response.data.appId === 'beijing') {
          console.info(this.topFilterList);
          this.topFilterList.find(x => x.fieldName === 'bjpjsx').hidden = false;
          this.columns.find(x => x.dataIndex === 'bjpjsx').hidden = false;
        }
      });
    },
    methods: {
      async getDictData() {
        let res= await getAllUser();
        console.info(res)
      },
      createTopFilterList() {
        return[
          {
            type: 'INPUT',
            label: '经销商代码',
            fieldName: 'jxcode',
            placeholder: '请选择',
            unitRender: () => {
              return (
                <div>
                  <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
                </div>
            );
            },
            change:p=>{
              if(!p){
                this.dealerCode=''
              }
            }
          },
          {
            type: 'INPUT',
            label: '仓库代码',
            fieldName: 'bsckdm'
          },
          {
            type: 'INPUT',
            label: '仓库名称',
            fieldName: 'bsckmc'
          },
          {
            type: 'SELECT',
            label: '状态',
            fieldName: 'xtqybz',
            placeholder: '请选择...',
            itemList: this.createDictList('1003')
          },
          {type: 'SELECT', label: '油电属性', fieldName: 'bjpjsx', placeholder: '请选择', hidden: true, itemList: this.createDictList('5000')}
        ]
      },
      createTableColumns () {
        return [
          {
            title: '序号',
            dataIndex: 'index',
            width: 70,
            sorter: true,
            render: props => {
              return <span>{props.row.index + 1}</span>;
            }
          },
          {
            title: '操作',
            dataIndex: 'column-action',
            width: 70,
            fixed: 'left',
            render: props => {
              return (
                <div>
                <el-button size="mini" type="text" onClick={() => this.editHandle(props.row, 'edit')}>
              编辑
              </el-button>
              </div>
            );
            }
          },
          {
            title: '油电属性',
            dataIndex: 'bjpjsx',
            sorter: true,
            dictItems: this.createDictList(5000),
          },
          {
            title: '经销商代码',
            dataIndex: 'jxcode',
            sorter: true,
          },
          {
            title: '经销商名称',
            dataIndex: 'jxname',
            sorter: true,
          },
          {
            title: '仓库代码',
            dataIndex: 'bsckdm',
            sorter: true,
          },
          {
            title: '仓库名称',
            dataIndex: 'bsckmc',
            sorter: true,
          },
          {
            title: '状态',
            dataIndex: 'xtqybz',
            sorter: true,
            render: props => {
              return <span>{this.createDictText(props.row.xtqybz, '1003')}</span>;
            }
          },
          {
            title: '创建人',
            dataIndex: 'createUserInfo.userName',
            sorter: true,
          },
          {
            title: '创建时间',
            dataIndex: 'createdAt',
            dateFormat: 'yyyy-MM-dd HH:mm:ss',
            sorter: true,
          },
          {
            title: '停用日期',
            dataIndex: 'tyrq',
            dateFormat: 'yyyy-MM-dd HH:mm:ss',
            sorter: true,
          }
        ]
      },
      selectChange(val) {
        console.log('val', val);
        if(val.length>0){
          console.info('111')
          this.isOpen=false;
          this.rowSelectData = val;
        } else {
          this.isOpen=true;
          this.rowSelectData = [];
        }
      },
      seacherFun(type){
        this.issearch = true
      },
      detailHandle(type) {
        this.title = '新增';
        this.handleType = 'add';
        this.initData = {
          type: type, // add-edit-show
          id: 1001
        }; //行id
        this.visibleDrawer = true;
      },
      editHandle(row, type) {
        if (type === 'edit') {
          this.title = '编辑';
        }
        this.formData = row;
        this.handleType = type;
        this.visibleDrawer = true;
      },
      async updateEnable() {
        this.saveLoading = true
        console.info(this.rowSelectData)
        let str = '';
        this.rowSelectData.map(x => {
          str += x.id + ',';
        });
        str = str.substring(0,str.length-1)
        console.log(str);
        const res = await piliangqiyong({ ids: str });
        this.fetchParams = { ...this.fetchParams };
        this.saveLoading = false
      },
      async updateStop() {
        this.saveLoading = true
        console.log(this.selectedRows);
        let str = '';
        this.rowSelectData.map(x => {
          str += x.id + ',';
        });
        str = str.substring(0,str.length-1)
        console.log(str);
        const res = await piliangtingyong({ ids: str });
        this.fetchParams = { ...this.fetchParams };
        this.saveLoading = false
      },
      changeHandle(val) {
        this.isOpen=true;
        this.fetchParams = Object.assign({}, val);
      },
      collapseHandle() {
        this.$nextTick(() => {
          this.BaseTable.EXECUTE_RESET_HEIGHT();
        });
      },
      dealercloseHandler(val){
        if(val){
          this.topFilterList.find(x=>x.fieldName=='jxcode').initialValue=val.dealerCode;
        }
        this.issearch=false;
      },
      infoClose(rData) {
        this.visibleDrawer = false;
        console.info(rData)
        if (rData.type === 'refresh') {
          // 重新加载
          this.fetchParams = { ...this.fetchParams };
        }
      },
    }
  }
</script>

<style scoped>

</style>
